import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
from datetime import datetime
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)
    # def test1(self):
        
    def login(self):
        '''
        商品登录
        '''
        self.browser.get("http://likeshop.abc/admin/account/login.html")
        sleep(2)
        # 输入框输入用户名
        self.browser.find_element(By.NAME,'account').send_keys('admin')
        # 输入密码
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        # 点击登录按钮
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        
    def test_product(self):
        """
         测试商品查询
        """
        self
        # 定位商品标签并点击商品 
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位子模块
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        # 切换到子模块
        self.browser.switch_to.frame(iframe)
        # 定位商品名称并且输入文具
        self.browser.find_element(By.NAME,'keyword').send_keys('文具')
        # 定位查询按钮并且点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)

        # 接受返回的商品列表
        is_not_bug=True
        product_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        
        if len(product_list) == 0:
            print('未查询到商品')
        else:
            # 遍历商品列表
            for product in product_list:
                # 定位商品名称
                product_name = product.find_element(By.XPATH,'./td[2]/div').text
                if '文具' not in product_name:
                    print('有bug')
                    is_not_bug=False
        if is_not_bug:
            print('没有bug')
                    
        # 退出子框架
        self.browser.switch_to.default_content()
    
    def test_logout(self):
        """
        测试用户退出
        """
        #定位用户信息
        dt = self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        # 鼠标悬停在用户信息上
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        # 定位退出按钮并点击退出
        self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)
        
    def test_product_add(self):
        """
        测试商品添加
        """
        # 定位商品标签并点击商品 
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位子模块
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        # 切换到子模块
        self.browser.switch_to.frame(iframe)
        # 定位发布商品按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(1)
        # 定位商品子框架
        iframe_product = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        # 切换到商品子框架
        self.browser.switch_to.frame(iframe_product)
        sleep(1)
        now = datetime.now().strftime('%Y%m%d%H%M%S')
        # 去除商品名称
        self.browser.find_element(By.NAME,'name').clear
        self.browser.find_element(By.NAME,'name').send_keys('测试商品'+now)
        
        # 点击第一个子分类
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()

        # 点击第二个子分类
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[4]').click()

        # 点击第三个子分类
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(1)
        
        # 定位并且点击添加商品图片
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(2)
        
        # 图片，轮播图上传方法
        def addimag(self):
            # 定位并且切换到图片子框架
            # 回到上一级框架
            self.browser.switch_to.parent_frame()
            sleep(2)
            iframe_img = self.browser.find_element(By.XPATH,'/html/body/div[6]/div[2]/iframe')
            self.browser.switch_to.frame(iframe_img)
            sleep(2)
            # 选择图片并且上传
            self.browser.find_element(By.CSS_SELECTOR, "input[type='file']").send_keys(r'D:\software_test\zhang\1.jpg')
            sleep(2)
            # 确定图片
            self.browser.find_element(By.XPATH,'//*[@id="lists"]/li[1]/div/img').click()
            self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
            sleep(2)
            
        # 图片上传
        addimag(self)
        # 定位并且点击轮播图
        # 切换到轮播图子框架
        self.browser.switch_to.parent_frame()
        self.browser.switch_to.frame(iframe_product)
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        # 轮播图上传
        addimag(self)
        
        
        self.browser.switch_to.parent_frame()
        iframe_product = self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        # 切换到商品子框架
        self.browser.switch_to.frame(iframe_product)
        sleep(1)
        # 定位价格库存
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('10')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        sleep(1)
        # 切换到销售设置
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[4]').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[4]/div[6]/div/div[1]/i').click()
        sleep(1)
        
        # 定位并且点击保存按钮
        self.browser.switch_to.parent_frame()
        sleep(1)
        self.browser.find_element(By.CLASS_NAME, "layui-layer-btn0").click()
        sleep(5)
        
        #进行上架操作
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(4)
        
        shelf_buttons = self.browser.find_elements(By.CSS_SELECTOR, "a[lay-event='status']")
        self.browser.execute_script("arguments[0].click();", shelf_buttons[0])
        sleep(5)
        self.browser.find_element(By.CSS_SELECTOR, ".layui-layer-btn0").click()
        
        
        
if __name__ == '__main__':
    lk=LikeShopTest()
    # lk.test1()
    lk.login()
    # lk.test_product()
    # lk.test_logout()
    lk.test_product_add()
    
    
    